<template>
  <div class="container">
    <div class="headerimg">
      <router-link to="/myblog" class="text1">personal blog</router-link>
      <div class="text2">路漫漫其修远兮,吾将上下而求索</div>
      <div class="text3">
        <router-link to="/myblog">首页</router-link>
        <a @click.prevent="linkclick('新闻')">新闻</a>
        <a @click.prevent="linkclick('科技')">科技</a>
        <a @click.prevent="linkclick('旅行')">旅行</a>
        <router-link to="">留言</router-link>
        <a @click.prevent="layout">退出</a>
      </div>
    </div>
    <el-card class="card1">
      <a @click="getsomeblog">推荐</a>
      <a v-for="item in categories" :key="item.id" @click.prevent="getcategory(item.alias)">{{ item.alias }}</a>
    </el-card>
    <div class="main">
      <div class="rankblog">
        <el-card class="card2">
          <h3 style="font-weight: bold">博客阅读量排行榜</h3>
          <div v-for="(item, index) in rankblog" :key="item.id" style="width: 100%; margin-bottom: 7px; font-weight: bold">
            <router-link :to="{ name: 'viewblog', params: { id: item.id } }">
              <span>{{ index + 1 }}.</span>{{ item.tittle }}</router-link
            >
          </div>
        </el-card>
        <el-card class="leaveword">
          <h3 style="font-weight: bold;;">留言板</h3>
          <div class="card4" v-for="item in leaveWords" :key="item.id">
            <img src="../assets/logo.png" style="width: 50px; height: 50px" alt="" />
            <div class="leaveContent">
              <p>留言人: {{ item.name }}</p>
              <p>内容: {{ item.content }}</p>
            </div>
          </div>
        </el-card>
      </div>
      <div class="news">
        <front-cover
          v-for="item in allblog"
          :id="item.id"
          :key="item.title"
          :coverUrl="item.image_address"
          :title="item.tittle"
          :summary="item.abstract"
          :author="item.author"
          :views="item.views"
          :createdAt="item.create_time"
        ></front-cover>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page"
          :page-sizes="[5, 7]"
          :page-size="size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>

      <div class="blogaside">
        <el-card>
          <p class="searchtext">搜索文章</p>
          <div class="search-container">
            <input type="text" class="search-input" placeholder="请输入关键字搜素" v-model="searchmsg" />
            <button class="search-button" @click="searchBlog">搜索</button>

            <p class="search-hint">搜素文章的标题</p>
          </div>
          <div class="hot-tags-container">
            <p class="searchtext">热门标签</p>
            <el-tag @click="getallblog">全部</el-tag>
            <el-tag v-for="tag in tagcategories" :key="tag.id" :closable="true" @close="removeTag(tag)" @click="tagclick(tag.alias)">{{ tag.alias }}</el-tag>
          </div>
          <div class="newblog">
            <h3 style="border-bottom: 1px solid black; width: 112px; margin-bottom: 5px; font-size: 16px; font-weight: bold">最新发布的博客</h3>
            <div class="newtittle" v-for="item in newblog" :key="item.id">
              <router-link :to="{ name: 'viewblog', params: { id: item.id } }"> {{ item.tittle }}</router-link>
            </div>
          </div>
        </el-card>
      </div>
    </div>
    <!-- <router-view></router-view> -->
  </div>
</template>

<script>
import FrontCover from '../components/BlogFrontCover.vue'
export default {
  name: 'BackEndMyBlog',
  components: { FrontCover },
  data() {
    return {
      categories: [],
      rankblog: [],
      allblog: [],
      newblog: [],
      tagcategories: [],
      page: 1,
      size: 5,
      total: 0,
      searchmsg: '',
      leaveWords: []
    }
  },

  mounted() {},
  async created() {
    this.getArticle()
    const {
      result: { data }
    } = await this.$http.get('/categories/all')

    this.categories = data
    this.categories.forEach(item => {
      this.tagcategories.push(item)
    })

    const { result } = await this.$http.get('/article/all')
    this.rankblog = result.data.sort(function (a, b) {
      b.views - a.views
    })
    this.rankblog = this.rankblog.reverse()
    const res = await this.$http.get('/article/newblog')
    this.newblog = res.result.data
    const leaveword = await this.$http.get('/BlogLeaveWords/all')
    this.leaveWords = leaveword.result.data
  },
  methods: {
    async getArticle() {
      // const { result } = await this.$http.get(`/article?page=${this.page}&size=${this.size}`)
      const { result, page } = await this.$http.get('/article', { params: { page: this.page, size: this.size } })
      this.page = Number(page)
      this.allblog = result.data
      this.total = result.total
    },
    removeTag(tag) {
      const index = this.tagcategories.indexOf(tag)
      if (index !== -1) {
        this.tagcategories.splice(index, 1)
      }
    },
    async tagclick(tag) {
      const searchMsg = tag
      const { result } = await this.$http.get('/article/search/category', { params: { page: this.page, size: this.size, searchMsg: searchMsg } })
      if (result.data.length == 0) {
        this.$message({
          type: 'error',
          message: '没有查询到相关分类的博客',
          duration: 1000
        })
        this.searchmsg = ''
        return
      }
      this.allblog = result.data
      this.searchmsg = ''
    },
    handleSizeChange(val) {
      this.size = val
      this.getArticle()
    },
    handleCurrentChange(val) {
      this.page = val
      this.getArticle()
    },
    async searchBlog() {
      const { result } = await this.$http.get('/article/search/blog', { params: { page: this.page, size: this.size, searchMsg: this.searchmsg } })
      if (result.data.length == 0) {
        this.$message({
          type: 'error',
          message: '没有查询到相关博客',
          duration: 1000
        })
        this.searchmsg = ''
        return
      }
      this.allblog = result.data
      this.searchmsg = ''
    },
    getallblog() {
      this.getArticle()
    },
    async linkclick(val) {
      const searchMsg = val
      const { result } = await this.$http.get('/article/search/category', { params: { page: this.page, size: this.size, searchMsg: searchMsg } })
      if (result.data.length == 0) {
        this.$message({
          type: 'error',
          message: '没有该分类的博客',
          duration: 1000
        })
        return
      }
      this.allblog = result.data
    },
    layout() {
      this.$message({
        type: 'success',
        message: '退出成功',
        duration: 1000
      })
      localStorage.removeItem('token')
      localStorage.removeItem('ismanager')
      this.$router.push('/login')
    },
    async getcategory(val) {
      const searchMsg = val
      const { result } = await this.$http.get('/article/search/category', { params: { page: this.page, size: this.size, searchMsg: searchMsg } })
      if (result.data.length == 0) {
        this.$message({
          type: 'error',
          message: '没有该分类的博客',
          duration: 1000
        })
        return
      }
      this.allblog = result.data
    },
    async getsomeblog() {
      const { result } = await this.$http.get('/article/someblog')
      this.allblog = result.data
    }
  }
}
</script>

<style scoped>
.headerimg {
  position: relative;
  width: 100%;
  /* background-color: #ddf; */
  height: 250px;
  background-image: url('../assets/images/preview.jpg');
  background-repeat: no-repeat;
  background-size: cover;
  background-attachment: fixed;
  background-position: center center;
}
.text1 {
  text-decoration: none;
  font-size: 20px;
  color: #fff;
  position: relative;
  left: 20px;
  top: 20px;
  font-weight: 900;
  font-family: 'WenQuanYi Micro Hei';
}
.text1:hover {
  color: rgb(230, 217, 217);
}
.text2 {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  color: rgb(163, 153, 153);
  font-size: 30px;
}
.text3 {
  float: right;
  position: relative;
  left: -20px;
  top: 20px;
}
.text3 a {
  text-decoration: none;
  font-size: 20px;
  color: #fff;
  font-weight: 700;
  font-family: 'WenQuanYi Micro Hei';
  padding: 0 10px;
  cursor: pointer;
}
.text3 a:hover {
  color: rgb(230, 217, 217);
}
.card1 a {
  text-decoration: none;
  font-size: 16px;
  font-family: 'WenQuanYi Micro Hei';
  padding: 0 10px;
  font-weight: 500;
  color: rgb(66, 64, 64);
  cursor: pointer;
}
.card2 {
  display: flex;
  /* width: 15%; */
  height: 410px;
  background-color: #f9f9f9;
  margin-top: 10px;
  margin-left: 20px;
}
.leaveword{
  display: flex;
  /* width: 15%; */
  height: 350px;
  background-color: #f9f9f9;
  margin-top: 10px;
  margin-left: 20px;
}
.rankblog {
  width: 400px;
}
.card2 a {
  text-decoration: none;
  font-size: 13px;
  color: black;
}
.card2 a:hover {
  color: #1ea3f6;
}
.card2 a span {
  font-size: 15px;
}
.card2 h3 {
  margin-bottom: 10px;
}
.main {
  display: flex;
  justify-content: space-between;
}
.news {
  width: 60%;
  margin-left: 20px;
  margin-top: 10px;
}
.blogaside {
  /* flex: 1; */
  width: 20%;
  margin-top: 10px;
  margin-left: 20px;
  min-width: 300px;
  height: 100%;
}
.texts {
  display: flex;
}
.blogtexts {
  margin-left: 5px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}
.search-form {
  display: flex;
  align-items: center;
}

.search-input {
  flex: 1;
  height: 32px;
  padding: 0 10px;
  border: 1px solid #ccc;
  border-radius: 16px;
  font-size: 14px;
}

.search-button {
  height: 32px;
  padding: 0 16px;
  border: none;
  background: #00bcd4;
  color: #fff;
  font-size: 14px;
  cursor: pointer;
}

.search-hint {
  margin-top: 10px;
  font-size: 12px;
  color: #999;
}
.searchtext {
  width: 65px;
  border-bottom: 1px solid #000;
  margin-bottom: 5px;
  font-weight: bold;
}
.hot-tags-container {
  margin-left: -20px;
  padding: 20px;
  background: #fff;
  margin-top: 30px;
}
.hot-tags-container .el-tag {
  margin-right: 10px;
  margin-bottom: 10px;
  cursor: pointer;
}
.hot-tags-container .el-tag:hover {
  border-color: #00bcd4;
  color: #00bcd4;
}
.newtittle {
  padding-bottom: 10px;
  font-size: 13px;
  font-weight: bold;
  padding-bottom: 10px;
  line-height: 1.5em;
}
.newblog{
  margin-top: 50px;
}
.newblog a {
  text-decoration: none;
  color: black;
}
.newblog a:hover {
  color: #1ea3f6;
}
.card4 {
  display: flex;
  margin-top: 20px;
}
.leaveContent {
  display: flex;
  margin-left: 10px;
  flex-direction: column;
  justify-content: space-around;
}
</style>
